package curd

import (
	"strings"

	"gitee.com/miajio/generator-code-for-go/pkg/utils"
)

var (
	dbStrType   = []string{"longtext", "mediumtext", "text", "varchar"}
	dbByteType  = []string{"binary", "blob", "longblob", "mediumblob", "varbinary"}
	dbIntType   = []string{"bigint", "int", "integer", "smallint"}
	dbBoolType  = []string{"bool"}
	dbDateType  = []string{"date", "datetime"}
	dbFloatType = []string{"double", "float", "numeric"}
)

// 将数据库类型转换为go类型
func dbTypeToGoType(dbType string) string {
	dbType = strings.ToLower(dbType)
	if utils.InArray(dbStrType, dbType) {
		return "string"
	}
	if utils.InArray(dbByteType, dbType) {
		return "[]byte"
	}
	if utils.InArray(dbIntType, dbType) {
		switch dbType {
		case "bigint":
			return "int64"
		case "int":
			return "int"
		case "integer":
			return "int8"
		case "smallint":
			return "int32"
		default:
			return "int"
		}
	}
	if utils.InArray(dbBoolType, dbType) {
		return "bool"
	}
	if utils.InArray(dbDateType, dbType) {
		return "time.Time"
	}
	if utils.InArray(dbFloatType, dbType) {
		return "float64"
	}
	return "string"
}
